<template>
	<view class="work-container">
		<uni-forms ref="form" :model="FormData" class="example">
			<uni-row>
				<uni-col :span="24">
					<uni-forms-item  name="pigFirstPrice">
						<uni-section>
							<uni-card title="基础卡片" extra="额外信息">
								<text class="uni-body">今日单价：{{FormData.pigFirstPrice}}</text>
							</uni-card>
						</uni-section>
					</uni-forms-item>
				</uni-col>
			</uni-row>
			<uni-row>
				<uni-col :span="16">
					<uni-forms-item label="出场类型" name="pigOutType">
						<uni-data-select
						v-model="FormData.pigOutType"
						:localdata="pigOutTypeList"
						:clear="false"
						/>
					</uni-forms-item>
				</uni-col>
			</uni-row>
			<uni-row>
				<uni-col :span="16">
					<uni-forms-item label="重量" name="pigWeight" required>
						<uni-easyinput
							v-model="FormData.pigWeight"
							suffixIcon="123"
							@input="getPrice"
						>
						<template #right><text style="margin-right: 24rpx;">kg</text></template>
						</uni-easyinput>
					</uni-forms-item>
				</uni-col>
			</uni-row>
			<uni-row>
				<uni-col :span="16">
					<uni-forms-item label="金额" name="pigPrice">
						<uni-easyinput v-model="FormData.pigPrice" suffixIcon="123">
							<template #right><text style="margin-right: 24rpx;">¥</text></template>
						</uni-easyinput>
					</uni-forms-item>
				</uni-col>
			</uni-row>

			<view @click="submitForm" class="submitButton">
				<uni-icons type="scan" size="80" color="#fff"></uni-icons>
			</view>
		</uni-forms>

	</view>

</template>

<script>
	import{ addPigOut }from "@/api/hmy/pigOut.js"
	export default {
		name: "yyhtest",
		data() {
			return {
				pigOutTypeList:[
					{value:1,text:"死亡"},
					{value:2,text:"育成"},
					{value:3,text:"转出"},
				],
				FormData:{
					pigOutType:2,
					pigFirstPrice:20.0,
					pigWeight:undefined,
					pigPrice:undefined,
				},
				rules: {
				  pigWeight: {
					rules: [{
					  required: true,
					  errorMessage: '重量'
					}]
				  }
				}

			};
		},
		onReady() {
		  this.$refs.form.setRules(this.rules)
		},
		created() {

		},
		methods: {
			submitForm(form){
				this.$refs.form.validate().then((res)=>{
					// 成功返回，res 为表单数据
					// 其他逻辑处理
					// ...
					//console.log(res);
					this.codeScan(res)
				}).catch((err)=>{
					// 表单校验验失败，err 为具体错误信息
					// 其他逻辑处理
					// ...
				})
			},
			codeScan(form) {
				//console.log(form);
				var _this = this;
				uni.scanCode({
					onlyFromCamera: true, //为true只允许相机扫码，不加允许相册扫码
					success: function(res) {
						let pigData = JSON.parse(String(res.result));
						form.pigCode = pigData.pigCode
						form.pigType = pigData.pigType
						form.phouseType = pigData.phouseType
						addPigOut(form).then(res =>{
							uni.showToast({
								title: '扫码成功'
							})
						})
					},
					fail: function(err) {
						console.log('扫码失败', err)
					}
				})
			},
			getPrice(obj){
				this.pigWeight = obj
				this.FormData.pigPrice = Number(obj) * this.FormData.pigFirstPrice
				// console.log(this.pigPrice)
			}
		}

	}
</script>

<style lang="scss">
	/* #ifndef APP-NVUE */
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		min-height: 100%;
		height: auto;
		background-color: #fcfcfc;
	}

	view {
		font-size: 16px;
		line-height: inherit;
	}

	/* #endif */
	.code {
		margin-bottom: 5px;
	}

	.icon {
		float: right;
		margin-right: 10px;
		margin-top: 5px;
	}

	.message {
		margin-top: 5px;
		color: #989897;
		line-height: 25px;
	}
	.example {
	  padding: 15px;
	  background-color: #fff;
	}
	.submitButton{
		width:fit-content;
		margin: 0 auto;
		padding: 10px;
		border-radius: 50%;
		background-color: #04BE02;
	}
</style>
